﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using VideoClassLibrary;
using VideoClassLibrary.BusinessLogicLayer;

public partial class Report_VideoUserEnableReprot : AdminUserBase
{
    private int recordCount = 0;
    string sqlwhere = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        //this.reportInfo.InnerHtml = new UserModule().GetUserLogInfo(this.SchoolID);
        if (!IsPostBack)
        {
            
            GetUserLogInfo(this.SchoolID,"");
            BindData(this.SchoolID, pager.StartRecordIndex, pager.PageSize, out recordCount,sqlwhere);
            pager.RecordCount = recordCount;
            bindTrancalss();
            BindCourseData();
        }


    }

    public  void  bindTrancalss()
    {
        int id = this.CurrentUser.SchoolID;
        TrainClassModule tcm = new TrainClassModule();
        DataSet ds = tcm.GetTrainclassListByWhere(id, "");
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            DataRow dr = null;
            ddlTrainClass.Items.Clear();
            ddlTrainClass.Items.Add(new ListItem("全部", "0"));
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dr = ds.Tables[0].Rows[i];
                ddlTrainClass.Items.Add(new ListItem(dr["VTC_name"].ToString(), dr["ID"].ToString()));
            }

        }
    }

    public void BindCourseData()
    {
        //int id = this.CurrentUser.SchoolID;
        //CourseModule tcm = new CourseModule();
        //DataSet ds = new DataSet();
        //if (this.ddlTrainClass.SelectedValue != "0" && ddlTrainClass.SelectedValue != "")
        //{
        //    ds = tcm.GetCourseListByWhere(id, int.Parse(this.ddlTrainClass.SelectedValue), "");
        //}
        //else
        //{
        //    ds = tcm.GetCourseListByWhere(id, "");
        //}
        //if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        //{
        //    DataRow dr = null;
        //    ddlCourse.Items.Clear();
        //    ddlCourse.Items.Add(new ListItem("全部课程", "0"));
        //    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        //    {
        //        dr = ds.Tables[0].Rows[i];
        //        ddlCourse.Items.Add(new ListItem(dr["VC_name"].ToString(), dr["ID"].ToString()));
        //    }

        //}
    }

    protected void btnCheckByTrainClass_Click(object sender, EventArgs e)
    {
        int trainclassid = Convert.ToInt32(ddlTrainClass.SelectedValue);
        //int courseid = Convert.ToInt32(ddlCourse.SelectedValue);
        if (ddlTrainClass.SelectedValue != "0")
        {
            sqlwhere = "  and vd.TrainClassID=" + trainclassid.ToString();
        }



        BindData(this.SchoolID, pager.StartRecordIndex, pager.PageSize, out recordCount, sqlwhere);
        GetUserLogInfo(this.SchoolID,sqlwhere);
        pager.RecordCount = recordCount;

    }

    public void GetUserLogInfo(int schoolId,string strWhr)
    {
        StringBuilder tableStr = new StringBuilder();
        string sqlStr = "";
        if (!string.IsNullOrEmpty(strWhr))
        {
            sqlStr = "select count(*) FROM V_user v  left join V_courselistDest vd on v.id=vd.userid where v.schoolid = @ID ";
            sqlStr = sqlStr + strWhr;
        }
        else
        {
            sqlStr = "select count(*) FROM [V_user] where schoolid = @ID ";
            
        }
        Database db = VDataBase.CreateDatabase();
        DbCommand command = db.GetSqlStringCommand(sqlStr);
        db.AddInParameter(command, "ID", DbType.Int32, schoolId);
        command.CommandText = sqlStr;
        int userCount = int.Parse(db.ExecuteScalar(command).ToString());
        if (!string.IsNullOrEmpty(strWhr))
        {
            sqlStr = "select count(*) FROM V_user v  left join V_courselistDest vd on v.id=vd.userid where v.schoolid = @ID  and IsAvailable = 1 ";
            sqlStr = sqlStr + strWhr;
        }
        else
        {
            sqlStr =
            "select count(*) FROM [V_user]  where schoolid = @ID and IsAvailable = 1 ";
        }
        
        command.CommandText = sqlStr;
        int availableCount = int.Parse(db.ExecuteScalar(command).ToString());
        if (!string.IsNullOrEmpty(strWhr))
        {
            sqlStr =
             "select COUNT(*) from v_user c left join V_courselistDest vd on c.id=vd.userid  where c.ID in( select distinct userID from V_UserLoginLog where userID in (select ID FROM [V_user] where schoolid = @ID ) and  ( Login_Time>='" +
             DateTime.Today.ToString("yyyy-MM-dd") + "' and Logout_Time < '" +
             DateTime.Today.AddDays(1).ToString("yyyy-MM-dd") + "'))";
            sqlStr = sqlStr + strWhr;
        }
        else
        {
            sqlStr =
              "select COUNT(*) from v_user c where c.ID in( select distinct userID from V_UserLoginLog where userID in (select ID FROM [V_user] where schoolid = @ID ) and  ( Login_Time>='" +
              DateTime.Today.ToString("yyyy-MM-dd") + "' and Logout_Time < '" +
              DateTime.Today.AddDays(1).ToString("yyyy-MM-dd") + "'))";
        }
        
        command.CommandText = sqlStr;
        int loginUser = int.Parse(db.ExecuteScalar(command).ToString());

        tableStr.Append("<tr><td ><font color=\"red\">用户总数：</font>" + userCount.ToString() + "</td>");
        tableStr.Append("<td> <font color=\"red\">启动用户数：</font>" + availableCount.ToString() + "</td>");
        tableStr.Append("<td> <font color=\"red\">今天活跃用户数：</font>" + loginUser.ToString() + "</td></tr>");
        ltrMsg.Text = tableStr.ToString();
    }

    

    public void BindData(int schoolId, int startIndex, int maxRow, out int record, string sqlwhere)
    {
        record = 0;
        string sqlStr = string.Empty;
           

 
        if(!string.IsNullOrEmpty(sqlwhere))
        {
            sqlStr=
    @" select distinct v.id,v.VU_loginname,v.IsAvailable,s.VS_name from V_user v left join V_courselistDest vd on v.ID=vd.UserID
  left join V_school s on v.SchoolID=s.ID
  where v.SchoolID=@ID and v.IsAvailable= 0";
            sqlStr = sqlStr + sqlwhere;
        }
        else
        {
            sqlStr = @"   select  v.id,v.VU_loginname,v.IsAvailable,s.VS_name from V_user v
  left join V_school s on v.SchoolID=s.ID
  where v.SchoolID=@ID and v.IsAvailable= 0  ";
        }


        Database db = VDataBase.CreateDatabase();

        DbCommand command = db.GetSqlStringCommand(sqlStr);
        command.CommandText = sqlStr;
        db.AddInParameter(command, "ID", DbType.Int32, schoolId);
        DataSet ds = db.ExecuteDataSet(command);
        if (ds.Tables[0].Rows.Count > 0)
        {
            var query =
                ds.Tables[0].AsEnumerable().Select(s=>new 
                                                       {
                                                           VU_loginname = s.Field<string>("VU_loginname"),
                                                           ID=s.Field<int>("ID"),
                                                           VS_Name = s.Field<string>("VS_name"),
                                                           IsAvailable = s.Field<bool>("IsAvailable")
                                                           

        }).OrderByDescending(s => s.ID).Skip(startIndex-1).Take(maxRow);
            record = ds.Tables[0].AsEnumerable().Count();
            Repeater1.DataSource = query.ToList();
            Repeater1.DataBind();
        }else
        {
            Repeater1.DataSource = "";
            Repeater1.DataBind();
            pager.RecordCount = 0;
        }
    }
    protected void pager_PageChanged(object sender, EventArgs e)
    {
        int trainclassid = Convert.ToInt32(ddlTrainClass.SelectedValue);
        //int courseid = Convert.ToInt32(ddlCourse.SelectedValue);
        if (ddlTrainClass.SelectedValue != "0")
        {
            sqlwhere = "  and vc.TrainClassID=" + trainclassid.ToString();
        }
        
       
        BindData(this.SchoolID, pager.StartRecordIndex, pager.PageSize, out recordCount,sqlwhere);
        pager.RecordCount = recordCount;

    }
}
